package cn.sccl.common.zabbix;

import cn.sccl.common.util.PropertiesUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.github.hengyunabc.zabbix.api.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * monitor api
 * Created by liupan on 2017/3/6.
 */
public class Zabbix {
    private Log log = LogFactory.getLog(Zabbix.class);

    @Value("${monitor.server.url}")
    private String url;
    @Value("${monitor.server.user}")
    private String user;
    @Value("${monitor.server.password}")
    private String password;

    private static volatile Zabbix zabbix;
    private ZabbixApi zabbixApi;

    private Zabbix() throws Exception {
        if (url == null || url.isEmpty()) {
            url = PropertiesUtil.getString("zabbix.server.url");
        }
        if (user == null || user.isEmpty()) {
            user = PropertiesUtil.getString("zabbix.server.user");
        }
        if (password == null || password.isEmpty()) {
            password = PropertiesUtil.getString("zabbix.server.password");
        }
        System.out.println("url = "+url);
        System.out.println("user = "+user);
        System.out.println("password = "+password);
        zabbixApi = new DefaultZabbixApi(url);
        zabbixApi.init();
        boolean login = zabbixApi.login(user,password);
        if (!login) {
            log.error("登陆zabbix服务器失败！");
            throw new Exception("登陆zabbix服务器失败！");
        }
    }

    public static Zabbix getZabbix() {
        if (zabbix == null) {
            synchronized (Zabbix.class) {
                if (zabbix == null) {
                    try {
                        zabbix = new Zabbix();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return zabbix;
    }


    public JSONObject call(String method, Map<String,Object> params) {
        RequestBuilder requestBuilder = RequestBuilder.newBuilder().method(method);
        Set<String> keySets =  params.keySet();
        for (String key: keySets) {
            Object value = params.get(key);
            requestBuilder.paramEntry(key, value);
        }

        Request request = requestBuilder.build();
        JSONObject result = zabbixApi.call(request);
//        System.out.println(JSON.toJSONString(result,true));
        return result;
    }

    public JSONObject call(String method, List params) {
        RequestBuilderArray requestBuilder = RequestBuilderArray.newBuilder().method(method);
        for (Object value: params) {
            requestBuilder.paramEntry(value);
        }

        RequestArray request = requestBuilder.build();
        JSONObject result = zabbixApi.call(request);
//        System.out.println(JSON.toJSONString(result,true));
        return result;
    }

}
